﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class cn_search : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string param = Request["key"];
        if (param == null || param.Equals(""))
            return;
        //System.Text.UTF8Encoding ut = new System.Text.UTF8Encoding();
        
        bindPage(param);
    }

    public void bindPage(string key)
    {
        if (key == null || key.Equals(""))
            return;
        DataSet ds = null;
        DB db = new DB();
        //页面首次加载时
        if (!Page.IsPostBack)
        {
            ds = db.search(key);
            DataView dv;
            dv = ds.Tables[0].DefaultView;
            AspNetPager1.RecordCount = dv.Table.Rows.Count;
            Cache.Insert("ds_productList", (DataView)dv, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 5, 0));
            bindData();
        }
    }

    private void checkCache()
    {
        if (Cache["ds_productList"] == null)
        {
            DB db = new DB();
            DataSet ds = null;
            DataView dv;
            ds = db.getAllProducts();
            dv = ds.Tables[0].DefaultView;
            AspNetPager1.RecordCount = dv.Table.Rows.Count;
            Cache.Insert("ds_productList", (DataView)dv, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 5, 0));
        }
    }

    private void bindData()
    {
        checkCache();
        PagedDataSource pds = new PagedDataSource();
        pds.AllowPaging = true;
        pds.PageSize = AspNetPager1.PageSize;
        pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
        pds.DataSource = (DataView)Cache["ds_productList"];
        productListRepeater.DataSource = pds;
        productListRepeater.DataBind();
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        bindData();
    }

    protected string cutstr(string strChar, int intLength)
    {
        //取得自定义长度的字符串
        if (strChar.Length > intLength)
        { return strChar.Substring(0, intLength); }
        else
        { return strChar; }

    }
}
